﻿using KhoaLuan.Areas.Admin.ActionFilters;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Areas.Admin.Controllers
{
  [Security]
    public class CategoryController : Controller
    {
        //
        // GET: /Admin/Category/
        LuanVanEntities db = new LuanVanEntities();
        int PageSize = 10;
        public ActionResult GetPage(int PageNo = 0)
        {
            ViewBag.Categorys = db.Categories.ToList()
                .Skip(PageSize * PageNo).Take(PageSize);
            return PartialView("_Category");
        }
        public ActionResult Index()
        {
            ViewBag.PageCount = Math.Ceiling(1.0 * db.Menus.ToList().Count / PageSize);
            var model = new Category();
            ViewBag.Categorys = db.Categories;
            return View(model);
        }
        public ActionResult Insert(Category model)
        {
           try
           {
               db.Categories.Add(model);
               db.SaveChanges();
               ModelState.AddModelError("", "Cập nhật thành công !");
               return RedirectToAction("Index");
           }
            catch(Exception ex)
           {
               ModelState.AddModelError("", ex.Message);
               ViewBag.Categorys = db.Categories;
              
               return View("Index", model);

           }
          
        }
        public ActionResult Update(Category model)
        {
            try
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                ModelState.AddModelError("", "Cập nhật Thành Công");

            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.Message);

            }
            ViewBag.Categorys = db.Categories;
            return View("Index", model);
        }
        public ActionResult Delete(int Id)
        {
            var model = db.Categories.Find(Id);
            db.Categories.Remove(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int Id)
        {
            var model = db.Categories.Find(Id);
            ViewBag.Categorys = db.Categories;
            return View("Index", model);
        }
	}
}